package com.dayuan.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dayuan.bean.UserInfo;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.UserInfoService;
import com.dayuan.util.PatternUtils;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/user")
public class RegisterController {

	

	@Resource(name = "userInfoService")
	private UserInfoService uis;

	/**
	 * 通过邮箱注册
	 * 
	 * @param ui
	 * @param passwordRepeat
	 * @return
	 */
	@RequestMapping(value = "/register_email.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo register_email(UserInfo ui, String passwordRepeat) {
		ResultVo resultVo = new ResultVo();
		try {
			if (ui.getEmail() == null || "".equals(ui.getEmail()) || ui.getPassword() == null
					|| "".equals(ui.getPassword())) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("帐号密码不能为空");
				return resultVo;
			} else if (!ui.getPassword().equals(passwordRepeat)) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("两次密码不一致");
				return resultVo;
			} else if (!PatternUtils.isEmail(ui.getEmail())) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("帐号不为邮箱");
				return resultVo;
			}

			/*
			 * if(uis.selectUser(ui).getValue().equals(AjaxMsg.LOGINSUCCESS)){
			 * resultVo.setMsg("帐号已被注册"); return resultVo; }
			 */

			uis.insertUser(ui);
			resultVo.setCode(AjaxMsg.REGISTER_SUCCESS.getValue());
			resultVo.setMsg("注册成功");
			return resultVo;

		} catch (Exception e) {
			resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
			resultVo.setMsg("网络不好");
			return resultVo;
		}

	}

	// 通过手机号码注册
	@RequestMapping(value = "/register_mobile.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo register_mobile(HttpSession session, UserInfo ui, String passwordRepeat, Integer code) {
		ResultVo resultVo = new ResultVo();
		try {
		
			
			if(code==null){
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("验证码不能为空");
				return resultVo;
			}
			Integer codeSession = session.getAttribute("code") == null ? -1 : (Integer) session.getAttribute("code");
	
			if (codeSession.intValue() != code.intValue()) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("验证码错误");
				
				session.removeAttribute("code");
				return resultVo;
			}

			if (ui.getMobile() == null || "".equals(ui.getMobile()) || ui.getPassword() == null
					|| "".equals(ui.getPassword())) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("帐号密码不能为空");
				return resultVo;
			} else if (!ui.getPassword().equals(passwordRepeat)) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("两次密码不一致");
				return resultVo;
			} else if (!PatternUtils.isMobile(ui.getMobile())) {
				resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
				resultVo.setMsg("帐号不是手机号码");
				return resultVo;
			}

			/*
			 * if(uis.selectUser(ui).getValue().equals(AjaxMsg.LOGINSUCCESS)){
			 * resultVo.setMsg("帐号已被注册"); return resultVo; }
			 */

			uis.insertUser(ui);
			resultVo.setCode(AjaxMsg.REGISTER_SUCCESS.getValue());
			resultVo.setMsg("注册成功");
			session.removeAttribute("code");
			return resultVo;

		} catch (Exception e) {
			resultVo.setCode(AjaxMsg.REGISTER_FAIL.getValue());
			resultVo.setMsg("网络不好");
			return resultVo;
		}

	}

	/**
	 * 生成6位验证码
	 * 
	 * @param session
	 */
	@RequestMapping(value = "/register_code.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo code(HttpSession session) {
		ResultVo resultVo = new ResultVo();
		int code = (int) ((Math.random() * 9 + 1) * 100000);
		System.out.println(code);
		session.setAttribute("code", code);
		resultVo.setMsg("验证码已发送");
		return resultVo;
	}

}
